<style type="text/css">
.button_select_all {float:left; width:100%; height:25px; line-height:25px;border-bottom: 1px solid #7394bf;color: #333;text-align: center;background: #e9eff5 none repeat scroll 0 0; }
</style>
<{if !$env.get.target}><div class="division" container="true"><{/if}>
<h3>关联物料</h3>
<input type="hidden" id="count" name="count" value="<{$count}>" />
<div class='frt' style="width:20%;margin-right:2%">
    <form action="index.php?app=o2o&ctl=admin_branch_product&act=dispatch" method="post" id="search_form">
        <input type="hidden" name="search" value="true" />
        <table>
            <tr>
                <td>
                    <select name="search_key" vtype="required" onchange="change_search(this)">
                        <{foreach from=$search item=item key=key}>
                            <option <{if $key== $search_key}>selected='selected'<{/if}> class="search" value="<{$key}>"><{$item}></option>
                        <{/foreach}>
                    </select>
                </td>
                <td>
                    <{input id="default_show" type="text" style="display:block" name="search_value" vtype="required" value="{$search_value}"}>
                    <{foreach from=$search_list item=items key=keys}>
                        <select vtype="required" id="show_<{$keys}>" style="display:none" name="search_value_<{$keys}>" vtype="required" >
                            <{foreach from=$items item=item key=key}>
                                <option <{if $key==$search_value_key.$keys}>selected='selected'<{/if}> value="<{$key}>"><{$item}></option>
                            <{/foreach}>
                        </select>
                    <{/foreach}>
                </td>
                <td>
                    <a class="finder-search-btn lnk-search" href="javascript:void(0);" onclick="do_search()">
                        <span><{t}>搜索<{/t}></span>
                    </a>
                </td>
            </tr>
        </table>
    </form>
</div>

<form action="index.php?app=o2o&ctl=admin_branch_product&act=do_save" method="post" id="goods_form">
<div style="float:left;width:60%;">
     <div style="float:left">
        <div style="margin:2px 16px 2px 0;padding: 4px;height:26px; width:100%;">
            <div style="float:left;">组织架构：</div>
            <div style="float:left;"><{input type='organization' app='organization' show="onlytree" effect="organization_stores_list"}></div>
        </div>
        <div style="margin:2px 16px 2px 0;padding: 4px;height:26px; width:100%;">
            <div style="float:left;">选择门店：</div>
            <div style="float:left;" id="organization_stores_list"></div>
            <div style="float:left;color:red">&nbsp;*<{help}><{t}>筛选特定门店请做选择,组织架构是用来过滤此门店下拉的列表。 <{/t}><{/help}></div>
        </div>
    </div>
</div>

<br />
<br />

<div id="button_select_all" class="button_select_all" style="display:none;">
    <span id="msg">&nbsp;</span>
    <span class="lnk" style="font-weight: bold;" onclick="no_select_all()">取消选定</span>&nbsp;
    <span class="lnk" style="font-weight: bold;" onclick="select_all()">选定全部</span>
</div>
    <input type="hidden" name="select_all" value="false" id="select_all" />
    <input type="hidden" name="search_key" value="<{$search_key}>"/>
    <input type="hidden" name="search_value" value="<{$search_value_last}>"/>

    <table cellpadding="0" cellspacing="0" border="0" class="gridlist">
        <thead>
            <tr>
            <th><input type="checkbox" name="selDataListAll" id="selDataListAll" onclick="selectAll()" /></th>
            <th>基础物料名称</th>
            <th>基础物料编码</th>
            <th>品牌</th>
            <th>分类</th>
            <th>规格</th>
            </tr>
        </thead>
        <tbody>
        <{foreach item=item from=$rows}>
            <tr>
                <td><input type="checkbox" onclick="selectone()" class="product_item" name="bm_id[]" value="<{$item.bm_id}>"></td>
                <td><{$item.material_name}></td>
                <td><{$item.material_bn}></td>
                <td><{$item.brand_name}></td>
                <td><{$item.type_name}></td>
                <td><{$item.specifications}></td>
            </tr>
        <{/foreach}>
        </tbody>
    </table>
    <div class="table-action">
        <{$pager}>
    </div>
</form>
<{if !$env.get.target}></div><{/if}>
<{if !$env.get.target}>
    <{capture name="footbar"}>
    <div class="table-action">
        <{button label="确定" type="button" id="submit_button" onclick="do_submit()"}>
    </div>
    <{/capture}>
<{/if}>
<script>
change_search();
//var i=0;
function selectAll(){
    rs = is_select_all();
    if(rs == false) return;
    if($('selDataListAll').checked){
        var j=0;
        $$('.product_item').each(function(el){
            el.set('checked','checked');
            el.set('name','bm_id[]');
            j++;
        });
        $('msg').set('text','选中当前'+j+'条数据！');
        $('button_select_all').set('style','display:block;');
    }else{
        $$('.product_item').each(function(el){
            el.set('checked','');
            el.set('name','');
        });
        $('button_select_all').set('style','display:none');
    }
    //i = i+1;
}
function select_all(){
    var count = $('count').get('value');
    $('select_all').set('value','true');
    $$('.product_item').each(function(el){
        el.set('checked','checked');
        el.set('name','bm_id[]');
        $('msg').set('text','选中全部'+count+'数据！');
    });
}
function no_select_all(){
    $('select_all').set('value','false');
    window.location.reload(1);
}
function selectone(el){
    rs = is_select_all();
    if(rs == false) return;
    var i=0;
    
    $$('.product_item').each(function(el){
        var ch = el.get('checked');
        if(ch === true){
            
            i++;
        }
    });
    
    if(i > 1){
        $('button_select_all').set('style','display:block;');
        $('msg').set('text','选中当前'+ i +'条数据！');
    }
    else
    {
        $('button_select_all').set('style','display:none;');
        $('msg').set('text','选中当前'+ i +'条数据！');
    }
}

function do_submit(flag){
    new Request({
        url:$('goods_form').get('action'),
        data:$('goods_form').toQueryString(),
        onRequest:function(){
           $('submit_button').set('disabled', 'true');
           $('submit_button').getElements('span')[1].set('text','提交中');
        },
        onComplete:function(re){
            opener.finderGroup['<{$env.get.finder_id}>'].refresh();
            window.close();
        }
    }).send();
    
}

function do_search(){
    new Request({
        url:$('search_form').get('action'),
        data:$('search_form').toQueryString(),
        onComplete:function(re){
            if(!re){
                alert('请输入搜索条件！');
                return;
            }
            $('main').set('html',re);
            change_search();
        }
    }).send();
}

function change_search(el){
    $$('.search').each(function(e){
        var selected = e.get('selected');
        if(selected === true ){
            var value = e.get('value');
            
            $('show_brand_name').set('style','display:none');
            $('show_type_name').set('style','display:none');
            $('default_show').set('style','display:none');
            if(value == 'type_name') {
               $('show_type_name').set('style','display:block');
               $('default_show').set('value','');
            }    
            if(value == 'brand_name'){
                $('show_brand_name').set('style','display:block');
                $('default_show').set('value','');
            }  
            if(value == 'material_bn' || value == 'material_name'){
                $('default_show').set('style','display:block');
            }  
        }
    });

    load_organization_stores_list("", "", "onload");
}

//不能按回车
document.onkeydown=function(event) {
    e = event ? event :(window.event ? window.event : null); 
    if(e.keyCode==13){ 
    //执行的方法 
    return false;
    } 
}

function is_select_all(){
    var flag = $('select_all').get('value');
    if(flag == 'true'){
        no_select_all();
        return false;
    }
    return true;
}

window.addEvent('domready', function() {
    //页面加载时 默认显示所有组织门店
    load_organization_stores_list("", "", "onload");
});

//根据组织架构加载选择门店的option
function load_organization_stores_list(org_id,value,action){
    if (action == "onchange" && org_id == '_NULL_') {
        //在选择过程中 选择了 “请选择...” 选择门店options 这里自动显示父类下拉框的门店信息
        var org_id = "";
        if (value) {
            var arr_cur_select = value.split(":");
            org_id = arr_cur_select[2];
        }
    }
    new Request({
          url : "index.php?app=o2o&ctl=admin_branch_product&act=organization_stores_list",
          async : false,
          method : 'post',
          data : {
              p_org_id : org_id,
          },
          onComplete : function(store_html) {
              $("organization_stores_list").set('html', store_html);
          }
      }).send();
}

function type_radio(obj)
{
    
}
</script>